/*     */ package com.kingdee.eas.lifetime.util.edcommonutils;
/*     */ 
/*     */ import com.kingdee.util.DateTimeUtils;
/*     */ import java.io.FileInputStream;
/*     */ import java.math.BigDecimal;
/*     */ import java.util.Date;
/*     */ import org.apache.poi.hssf.usermodel.HSSFCell;
/*     */ import org.apache.poi.hssf.usermodel.HSSFDateUtil;
/*     */ import org.apache.poi.hssf.usermodel.HSSFRichTextString;
/*     */ import org.apache.poi.hssf.usermodel.HSSFRow;
/*     */ import org.apache.poi.hssf.usermodel.HSSFSheet;
/*     */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*     */ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/*     */ 
/*     */ public class ExcelUtil
/*     */ {
/*     */   public static HSSFWorkbook load(String filePath)
/*     */     throws Exception
/*     */   {
/*  26 */     FileInputStream fileInputStream = new FileInputStream(filePath);
/*  27 */     POIFSFileSystem fs = new POIFSFileSystem(fileInputStream);
/*  28 */     HSSFWorkbook wb = new HSSFWorkbook(fs);
/*  29 */     fileInputStream.close();
/*  30 */     return wb;
/*     */   }
/*     */ 
/*     */   public static HSSFSheet[] getSheets(String filePath) throws Exception {
/*  34 */     HSSFWorkbook wb = load(filePath);
/*  35 */     int tabs = getSheetTabs(wb);
/*  36 */     if (tabs > 0) {
/*  37 */       HSSFSheet[] sheets = new HSSFSheet[tabs];
/*  38 */       for (int i = 0; i < tabs; i++) {
/*  39 */         sheets[i] = wb.getSheetAt(i);
/*     */       }
/*  41 */       return sheets;
/*     */     }
/*  43 */     return null;
/*     */   }
/*     */ 
/*     */   public static String[] getSheetNames(String filePath) throws Exception {
/*  47 */     HSSFWorkbook wb = load(filePath);
/*  48 */     int tabs = getSheetTabs(wb);
/*  49 */     if (tabs > 0) {
/*  50 */       String[] sheets = new String[tabs];
/*  51 */       for (int i = 0; i < tabs; i++) {
/*  52 */         sheets[i] = wb.getSheetName(i);
/*     */       }
/*  54 */       return sheets;
/*     */     }
/*  56 */     return null;
/*     */   }
/*     */ 
/*     */   public static int getSheetTabs(HSSFWorkbook wb) {
/*  60 */     int index = 0;
/*     */     try {
/*     */       while (true) {
/*  63 */         String name = wb.getSheetName(index);
/*  64 */         if (name != null)
/*  65 */           index++; 
/*     */       }
/*     */     } catch (RuntimeException e) {
/*     */     }
/*  68 */     return index;
/*     */   }
/*     */ 
/*     */   public static int getColumnCount(HSSFRow hssfRow)
/*     */   {
/*  74 */     short index = 0;
/*     */     while (true) {
/*  76 */       HSSFCell cell = hssfRow.getCell(index);
/*  77 */       if ((cell == null) || (cell.getRichStringCellValue() == null) || (cell.getRichStringCellValue().getString() == null) || (cell.getRichStringCellValue().getString().trim().length() == 0)) break;
/*  78 */       index = (short)(index + 1);
/*     */     }
/*     */ 
/*  84 */     return index;
/*     */   }
/*     */ 
/*     */   public static String getCellValue(HSSFCell cell) {
/*  88 */     if (cell == null)
/*  89 */       return "";
/*  90 */     switch (cell.getCellType()) {
/*     */     case 1:
/*  92 */       return cell.getRichStringCellValue() != null ? cell.getRichStringCellValue().getString().trim() : "";
/*     */     case 0:
/*  95 */       double d = cell.getNumericCellValue();
/*  96 */       if (HSSFDateUtil.isCellDateFormatted(cell)) {
/*  97 */         Date cal = HSSFDateUtil.getJavaDate(d);
/*  98 */         return DateTimeUtils.format(cal, "yyyy-MM-dd");
/*     */       }
/* 100 */       BigDecimal bd = new BigDecimal(new Double(d).toString());
/* 101 */       String s = bd.toString();
/* 102 */       if (s.endsWith(".0"))
/* 103 */         s = s.substring(0, s.length() - 2);
/* 104 */       return s;
/*     */     case 2:
/* 107 */       String valueStr = null;
/* 108 */       valueStr = cell.getRichStringCellValue().getString();
/* 109 */       if ((valueStr != null) && (valueStr.trim().length() > 0))
/* 110 */         return valueStr.trim();
/* 111 */       double value = cell.getNumericCellValue();
/* 112 */       bd = new BigDecimal(new Double(value).toString());
/* 113 */       valueStr = bd.toString();
/* 114 */       if (valueStr.endsWith(".0"))
/* 115 */         valueStr = valueStr.substring(0, valueStr.length() - 2);
/* 116 */       return valueStr;
/*     */     case 4:
/* 119 */       return String.valueOf(cell.getBooleanCellValue());
/*     */     case 3:
/* 122 */       return "";
/*     */     }
/*     */ 
/* 125 */     return cell.getRichStringCellValue() != null ? cell.getRichStringCellValue().getString().trim() : "";
/*     */   }
/*     */ }

/* Location:           D:\work\kingdee\enable\富国基金\fuguo_lib.jar
 * Qualified Name:     com.kingdee.eas.lifetime.util.edcommonutils.ExcelUtil
 * JD-Core Version:    0.6.0
 */